package com.ffcs.crmd.tsp.server.netty.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.heart.HeartbeatData;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.application.ApplicationManagerHolder;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.manager.ClientRemotingManager;
import com.ffcs.crmd.tsp.server.manager.TaskRemotingManager;
import com.ffcs.crmd.tsp.server.netty.client.ClientRegisterInfo;
import com.ffcs.crmd.tsp.server.netty.task.TaskRegisterInfo;

import io.netty.channel.ChannelHandlerContext;

/**
 * 
 * 功能说明:心跳处理者
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class HeartDataProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger         log                   = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 执行端通信管理者
     */
    private TaskRemotingManager         taskRemotingManager   = ApplicationManagerHolder.getBean("taskRemotingManager",
        TaskRemotingManager.class);
    
    /**
     * 客户端管理器
     */
    private final ClientRemotingManager clientRemotingManager = ApplicationManagerHolder.getBean("clientRemotingManager",
        ClientRemotingManager.class);
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public HeartDataProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        HeartbeatData heartbeatData = (HeartbeatData) XmlUtil.fromXml(new String(request.getBody()), HeartbeatData.class);
        if (heartbeatData.getClientData() != null) {
            this.doClientHeartBeat(ctx, request, heartbeatData);
        }
        
        if (heartbeatData.getTaskData() != null) {
            this.doTaskHeartBeat(ctx, request, heartbeatData);
        }
        return null;
    }
    
    private void doClientHeartBeat(ChannelHandlerContext ctx, RemotingCommand request, HeartbeatData heartbeatData) {
        log.info("SERVER-NETTY : recive a heart beat from client [{}] channel[{}]", heartbeatData.getClientID(), ctx.channel());
        ClientRegisterInfo clientChannelInfo = new ClientRegisterInfo(ctx.channel(), heartbeatData.getClientID(), heartbeatData.getVersion());
        clientRemotingManager.registerClient(ctx.channel(), clientChannelInfo);
        
    }
    
    private void doTaskHeartBeat(ChannelHandlerContext ctx, RemotingCommand request, HeartbeatData heartbeatData) {
        log.info("SERVER-NETTY : recive a heart beat from task [{}] identity[{}] channel[{}]", heartbeatData.getClientID(),
            heartbeatData.getTaskData().getIdentity(), ctx.channel());
        TaskRegisterInfo taskChannelInfo = new TaskRegisterInfo(heartbeatData.getClientID(), heartbeatData.getTaskData().getIdentity(),
            heartbeatData.getVersion());
        taskRemotingManager.registerTask(ctx.channel(), taskChannelInfo);
    }
    
}
